package edu.zhangxu.servlet;

import edu.zhangxu.pojo.Result;
import edu.zhangxu.pojo.SubmitRecordResults;
import edu.zhangxu.service.SubmitRecordService;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SubmitRecordServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page = Integer.parseInt(req.getParameter("page"));
        resp.setContentType("application/json;charset=utf-8");
        resp.setHeader("Access-Control-Allow-Origin", "*");

        JSONObject jo = null;
        try {
            SubmitRecordResults res = new SubmitRecordResults();
            res.setState(0);
            SubmitRecordService srs = new SubmitRecordService();
            res.setTotal(srs.getSubmitRecordTotal());
            res.setList(srs.getSubmitRecords(page));
            jo = JSONObject.fromObject(res);
        } catch (Exception e) {
            jo = JSONObject.fromObject(new Result(-1, e.toString()));
        }
        resp.getWriter().print(jo);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
